﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

using DslModeling = global::Microsoft.VisualStudio.Modeling;

namespace devtm.Editor.TypeSelector
{
    public class AssemblyTypeProvider : ITypeProvider
    {

        public AssemblyTypeProvider(Assembly assembly) : this()
        {
            Assembly = assembly;
            SourceTypes.Source.Add(this);
        }

        public AssemblyTypeProvider()
        {
            //SourceTypes.Source.Add(this);
        }

        public Assembly Assembly { get; set; }

        public IEnumerable<System.Type> GetTypes(DslModeling.Store store, HashSet<String> namespaces)
        {
            return Assembly.GetTypes().Where(c => namespaces.Contains(c.Namespace));
        }

        public IEnumerable<Type> GetTypes(DslModeling.Store store, HashSet<String> namespaces, Func<Type, bool> filter)
        {
            foreach (Type item in Assembly.GetTypes().Where(c => namespaces.Contains(c.Namespace)))
                if (filter(item))
                    yield return item;
        }


        
        #region Disposable

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        ~AssemblyTypeProvider()
        {
            Dispose(false);
        }

        // The bulk of the clean-up code is implemented in Dispose(bool)
        protected virtual void Dispose(bool disposing)
        {

            if (disposing)
            {
                // free managed resources
            
            }

            //SourceTypes.Source.Remove(this);

        }

        #endregion
    }
}
